from datetime import time
import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By


# 公共方法文件
class WebKeys:

    # 浏览器运行，初始化  只要实例化webkey，就会走到init函数里面  打开浏览器
    def __init__(self, driver):
        self.driver = driver
        # self.driver.maximize_window()

        self.wait = WebDriverWait(self.driver, 5)

    # 访问  函数传参
    def open(self, url):
        self.driver.get(url)

    # 找元素  等待(显示等待)
    def locator_with_wait(self, name, value):
        locator = (name, value)
        el = self.wait.until(ec.visibility_of_element_located(locator))
        self.locator_yanshe(el)
        return el

    # 不确定是否找到元素，所以先查看一下定位颜色
    def locator_yanshe(self, el):
        self.driver.execute_script(
            "arguments[0].setAttribute('style',arguments[1]);",
            el,
            'border:1px solid green;'
        )

    # 点击
    def one_click(self, name, value):
        self.locator_with_wait(name, value).click()

    # 输入
    def one_input(self, name, value, center):
        self.locator_with_wait(name, value).send_keys(center)

    # 定时时间
    def leep(self, center):
        time.sleep(center)

    # 切换 iframe  窗口
    def iframe_que(self):
        self.driver.switch_to.frame()

    # 退出 iframe  窗口
    def iframe_tui(self):
        self.driver.switch_to.default_content()

    # 获取登录后的文本值  lambda x: x.find_element().text
    def get_text_true_wait(self, name, value, txt):
        pong = (name, value)
        result = self.wait.until(ec.text_to_be_present_in_element(pong, txt))
        return result
